<?php

namespace App\Logics;


use App\Models\NewsComment;
use App\Models\User;
use App\Service\Base\Logic;


class NewsCommentLogic extends Logic
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = '';
    }


    // 列表查询
    public function onLists()
    {
        $opt     = $this->option();
        $page    = $opt->getData('page', 1, 'intval');
        $rows    = $opt->getData('rows', 15, 'intval');
        $news_id = $opt->getData('news_id', 0, 'intval');
        $uid     = $opt->getData('uid', 0, 'intval');


        // 模型 和 查询  ->isCache($this->listCache)
        $where = [['verify', '=', 1]];
        $bang  = ['verify'];
        if($news_id){
            $bang[]  = 'news_id';
            $where[] = ['news_id', '=', $news_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }

        // 模型
        $model  = new NewsComment();
        $KCache = $model->cacheKey('lists', $where);
        $lists  = $model->cacheClass()->getCache($KCache);
        // 检查是否已经绑定
        if(!empty($lists)){
            $model->checkBangList($lists, $KCache);
        }

        if(empty($lists)){
            //$result = $model->bangField($bang)->listPage($where, $page, $rows, "id DESC");
            $start = ($page - 1) * $rows;
            $lists = $model->bangField($bang)->getList($where, $rows, "id DESC", $start);
            $lists = is_object($lists) ? $lists->toArray() : $lists;
            if($where != false){
                $model->addCacheList($lists, $KCache, $lists, 'list');
            }
        }
        // 处理评论
        if(!empty($lists)){
            $uids  = array_column($lists, 'uid');
            $users = (new User())->isCache(true)->getListInIds($uids);
            foreach($lists as &$item){
                $_uid = $item['uid'];
                // 匹配用户
                $item['user'] = $users[$_uid] ?? [];
            }
        }

        return $lists;
    }

    // 软删除（暂未使用）
    public function _toSoft($myUid)
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('select_operation_id');
        }

        $where  = ['id' => $_id, 'uid' => $myUid];
        $model  = new NewsComment();
        $result = $model->isCache(true)->oneEdit($where, ['verify' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 改变（直接修改，软删除，改变状态，排序）
    public function onEdit($myUid)
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('select_operation_id');
        }

        $opt  = $this->option();
        $data = [
            //'id'      => $opt->getData("id", null, "intval"),
            'news_id'    => $opt->getData("news_id", null, "intval"),
            'pid'        => $opt->getData("pid", null, "intval"),
            'uid'        => $myUid,
            'en_content' => $opt->getData("en_content", null, "trim"),
            'cn_content' => $opt->getData("cn_content", null, "trim"),
            'tc_content' => $opt->getData("tc_content", null, "trim"),
            //'author_ip'  => $opt->getIp(),
            //'verify'  => $opt->getData("verify", 1, "intval"),
            //'ctime'   => $opt->getData("ctime", null, "intval"),
            //'mtime'   => $opt->getData("mtime", null, "intval"),
        ];

        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('add_the_saved_data');
        }

        // 组合数据
        $where = ['id' => $_id, 'uid' => $myUid];
        $model = new NewsComment();
        $data  = $model->checkData($data, $where);
        if($data === false){
            return $this->_returnFail('add_the_saved_data');
        }

        // 验证
        $result = $model->checkRules($data, 'edit');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->oneEdit($where, $data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 树状
    public function onAdd($myUid)
    {
        $opt  = $this->option();
        $data = [
            //'id'      => $opt->getData("id", null, "intval"),
            'news_id'    => $opt->getData("news_id", null, "intval"),
            'pid'        => $opt->getData("pid", null, "intval"),
            'uid'        => $myUid,
            'en_content' => $opt->getData("en_content", null, "trim"),
            'cn_content' => $opt->getData("cn_content", null, "trim"),
            'tc_content' => $opt->getData("tc_content", null, "trim"),
            'author_ip'  => $opt->getIp(),
            //'verify'  => $opt->getData("verify", 1, "intval"),
            //'ctime'   => $opt->getData("ctime", null, "intval"),
            //'mtime'   => $opt->getData("mtime", null, "intval"),
        ];

        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('add_the_saved_data');
        }

        // 组合数据
        $model = new NewsComment();
        $data  = $model->checkData($data);
        if($data === false){
            return $this->_returnFail('add_the_saved_data');
        }

        // 验证
        $result = $model->checkRules($data, 'add');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->oneAdd($data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

}


